﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using TSharp.Core.Mvc;
using TSharp.Core.Mvc.XmlRouting;
using TSharp.Core.Osgi;

[assembly: TSharp.Core.Osgi.RegExtensionPoint(typeof(RouteManager))]

namespace TSharp.Core.Mvc
{
    /// <summary>
    /// 程序事件管理
    /// </summary>
    /// <author>
    /// tangjingbo
    /// </author>
    internal class RouteManager : ExtensionPoint<RegRouteAttribute>
    {
        private static readonly List<RegRouteAttribute> AllHandler = new List<RegRouteAttribute>(50);


        /// <summary>
        /// Registers the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="attribute">The attribute.</param>
        protected override void Register(System.Reflection.Assembly assembly, RegRouteAttribute attribute)
        {
            AllHandler.Add(attribute);
        }

        /// <summary>
        /// Uns the register.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="attribute">The attribute.</param>
        protected override void UnRegister(System.Reflection.Assembly assembly, RegRouteAttribute attribute)
        {
            AllHandler.Remove(attribute);
        }
        protected override void OnInit()
        {
            using (RouteTable.Routes.GetWriteLock())
            {
                //RouteTable.Routes.Clear();
                RouteTable.Routes.MapRouteFromConfig(false, "RouteConfiguration");
                AllHandler.ForEach(x => x.New().RegisterRoutes(RouteTable.Routes));
                RouteTable.Routes.MapRouteFromConfig(true, "RouteConfiguration");
            }
            base.OnInit();
        }





    }
}